package com.by.controller;

import com.alibaba.fastjson2.JSONObject;
import com.by.ResultBean;
import com.by.config.WxConfig;
import com.by.config.WxConstant;
import com.by.core.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @author ServivE
 * @version 0.1
 * @className UserController
 * @description
 * @date 2024/11/18 15:04
 * @since jdk17
 */
@RestController
@RequestMapping("/api")
public class UserController extends BaseController {
    @Autowired
    WxConfig wxConfig;

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/user/session")
    public ResultBean getSession(@RequestParam(required = true) String code) {
        String url = WxConstant.code2Session + "?appid=" + wxConfig.getAppid() + "&secret=" + wxConfig.getSecret() + "&js_code=" + code + "&grant_type=" + wxConfig.getGrant_type();
        String rs = restTemplate.getForObject(url, String.class);
        //字符串转换为json对象
        Map map = JSONObject.parseObject(rs, Map.class);
        return success("会话信息", map);
    }

}
